package com.server;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author wwf
 * @date 2020年12月25日 11:07:19
 */
public class Request {
    private String url;//请求地址
    private String partUrl;//方法地址
    private String host;//请求主机
    private String method;//请求方法
    private String reqcontent;//请求所有内容
    private String appName;//项目名字
    public Request(InputStream inputStream) throws IOException {
        int available = 0;
        while (available == 0){
            available = inputStream.available();

        }
        byte[] bt = new byte[available];
        inputStream.read(bt);
        this.reqcontent = new String(bt,"UTF-8");
        String[] reqContentArr = this.reqcontent.split("\\n");
        if(reqContentArr.length>2){
            String firstReqHeaderContent = reqContentArr[0];
            if(firstReqHeaderContent != null && firstReqHeaderContent != ""){
                String[] firstReqHeaderContentArr = firstReqHeaderContent.split(" ");
                if(firstReqHeaderContentArr.length==3){
                    this.method = firstReqHeaderContentArr[0];
                    this.url= firstReqHeaderContentArr[1];
                    String[] spliturl = this.url.split("/");
                    this.appName = spliturl[1];
                    this.partUrl = this.url.substring(this.url.indexOf(this.appName)+this.appName.length());
                    //this.partUrl = this.url.replace("/"+this.appName,"");
                }
            }
            String twoReqHeaderContent = reqContentArr[1];
            if(twoReqHeaderContent != null && twoReqHeaderContent != ""){
                String[] twoReqHeaderContentArr = twoReqHeaderContent.split(" ");
                if(twoReqHeaderContentArr.length == 2){
                    host = twoReqHeaderContentArr[1].replace("\r","");
                }
            }
        }

    }
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getReqcontent() {
        return reqcontent;
    }

    public void setReqcontent(String reqcontent) {
        this.reqcontent = reqcontent;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getPartUrl() {
        return partUrl;
    }

    public void setPartUrl(String partUrl) {
        this.partUrl = partUrl;
    }
}
